/*		
		---------RSS2Speech------------
		--Wasantha Wickramasinghe,CSE--
		--Level 3 Programming Project--

This is the script which contains the application logic of the RSS2Speech extension
RSS2Speech is an extension for Mozilla Firefox which speaks out RSS feed you have
subscribed via livebookmarks of Firefox

CLC4_TTS open source Text to Speech library is used to produce the sound output
All the function calls begins with CLC_ are functions of that library


*/



//firefox services required to access the RSS subscription detains
const livemarkService = Components.classes["@mozilla.org/browser/livemark-service;2"].getService(Components.interfaces.nsILivemarkService);
const bookmarkService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Components.interfaces.nsINavBookmarksService);

//get the array of subscribed live bookmarks
const anno = Components.classes["@mozilla.org/browser/annotation-service;1"].getService(Components.interfaces.nsIAnnotationService);
const livemarkIds = anno.getItemsWithAnnotation("livemark/feedURI", {});

const alertsService = Components.classes["@mozilla.org/alerts-service;1"]
                              .getService(Components.interfaces.nsIAlertsService);



var RSS=new Array();/*This is the array which contains RSS Channel objects. In each of those objects there are a number of RSS item objects
					which carry news items	
					*/




var livemark_count;	//counts the number of firefox live bookmarks

var rss_count;		//keep track of the currently playing RSS item

var channel_no;		//keep track of the current RSS channel

var play_status=1;	//set to playable initially. 1-playing,2-paused

var started=0;		//extension doesn't start talking RSS upon loading.This used to indicate whether it's stared or not
					//0-not started,1-started
					
					
					
//load_feeds() is executed when firefox is loaded					
window.addEventListener("load", function() {load_feeds() ; }, false);	

				
//this functions goes through the set of live bookmarks					
function load_feeds(){
var feedURL;
livemark_count=0;
for(j=0;j<livemarkIds.length;j++){
feedURL = livemarkService.getFeedURI(livemarkIds[livemark_count]).spec;
get_livemark(feedURL);
livemark_count++;
}


}


//this function fetches all the xml files containing rss contect from the 
//romote servers

function get_livemark(feed){


var feedName = bookmarkService.getItemTitle(livemarkIds[livemark_count]);


 var req = new XMLHttpRequest();  
 req.open('GET', feed , true); 
 req.setRequestHeader("Cache-Control", "no-cache");
 req.setRequestHeader("Pragma", "no-cache"); 
 req.onreadystatechange = function (aEvt) {  
   if (req.readyState == 4) {  
      if(req.status == 200){
		if (req.responseText != null){
					processRSS(req.responseXML);
					//alert("ok");
					
		}
		else
		{
			alert("Failed to receive RSS file from the server - file not found.");
			return false;
		}
		}	  

      else  
       dump("Error loading page\n");  
   }  
 };  
 req.send(null); 
}


function processRSS(rssxml)
{
	
	var rss_channel=new RSS2Channel(rssxml); //creates a RSS2Channel object from
											 //from the data in the xml file
	RSS.push(rss_channel); //inserts RSS2Channel objects to the RSS array

}
					

function RSS2Channel(rssxml)
{


	//array of RSS2Item objects
	this.items = new Array();
	this.name="Wasantha";



	var itemElements = rssxml.getElementsByTagName("item");


	for (var i=0; i<itemElements.length; i++)
	{
		Item = new RSS2Item(itemElements[i]);
		this.items.push(Item);

	}



}

function RSS2Item(itemxml)
{
	//fields containing RSS data
	this.title;
	this.link;
	this.description;





	var properties = new Array("title", "link", "description");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = itemxml.getElementsByTagName(properties[i])[0];
		if (tmpElement != null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	

	
}

function showRSS(){


		channel_no=0;
		rss_count=0;
		started=1;
		speak_rss();
	
}



function speak_rss(){
if(play_status==1){
document.getElementById('rss2speech-label').setAttribute("label",RSS[channel_no].items[rss_count].title);
document.getElementById('rss2speech-label').setAttribute("tooltiptext",RSS[channel_no].items[rss_count].description);
speak_it(RSS[channel_no].items[rss_count].title);
rss_count++;

if(rss_count < RSS[channel_no].items.length){
var t=setTimeout("speak_rss()",10000);
}
else{
	if(channel_no<(RSS.length-1)){
	channel_no++;
	rss_count=0;
	var t=setTimeout("speak_rss()",10000);
	}
}
}
}

function speak_it(thing_to_say){

CLC_Init(1);
if(CLC_Ready()){
CLC_Say(thing_to_say,0);
}

}


					
function play_pause(){
if(started==1){
if(play_status==1){
	play_status=0;

	document.getElementById('rss2speech-playimage').setAttribute("src","chrome://rss2speech/skin/button-play.png");
	document.getElementById('rss2speech-playimage').setAttribute("tooltiptext","Play");

}
else{
	play_status=1;
	
	document.getElementById('rss2speech-playimage').setAttribute("src","chrome://rss2speech/skin/button-pause.png");
	document.getElementById('rss2speech-playimage').setAttribute("tooltiptext","Pause");
	speak_rss();
}
}
else{
document.getElementById('rss2speech-playimage').setAttribute("src","chrome://rss2speech/skin/button-pause.png");
document.getElementById('rss2speech-playimage').setAttribute("tooltiptext","Pause");
showRSS();}
}


function prevChannel(){
if(channel_no>0){
	channel_no--;
	rss_count=0;
}
else{
	alertsService.showAlertNotification("chrome://rss2speech/skin/rss_img.png", 
                                    "", "No more channels", 
                                    false, "", null);

}
}



function nextChannel(){

if(channel_no<(RSS.length-1)){
	channel_no++;
	rss_count=0;
}
else{
		alertsService.showAlertNotification("chrome://rss2speech/skin/rss_img.png", 
                                    "", "No more channels", 
                                    false, "", null);
} 

}



function openLink(){

newTab(RSS[channel_no].items[rss_count-1].link);

}



function newTab( url ) {
var browser = top.document.getElementById("content");
var newtab = browser.addTab(decodeURIComponent(url));
browser.selectedTab = newtab;
}






















